package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.po.Action;
import com.cn.goodsadmin.po.Balance;
import com.cn.goodsadmin.service.ActionService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class ActionController {

    @Autowired
    ActionService actionService;

    @LoginRequired
    @RequestMapping(value = "/addaction", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addAction(@RequestBody Action action) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            actionService.addAction(action);
            actionService.sendUpdateFixtimeMessage(action);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/updateaction", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateAction(@RequestBody Action action) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            actionService.updateAction(action);
            actionService.sendUpdateFixtimeMessage(action);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/deleteaction", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteAction(@RequestBody Action action) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            actionService.deleteAction(action);
            actionService.sendUpdateFixtimeMessage(action);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/getallaction", method = RequestMethod.GET,params = {"appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getAllAction(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Action> actions = actionService.getAllAction(appid);
            if (actions != null) {
                supplyMap.put("actions", actions);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/getallbalance", method = RequestMethod.GET)
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getAllBalance() {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Balance> balances = actionService.getAllBalance();
            if (balances != null) {
                supplyMap.put("balances", balances);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


}
